<?php
include_once ("../dao/conexao_class.php");
class UrgenciaDAO{
    
    private $insert = "INSERT INTO urgencia(urg_nome, urg_cor) VALUE (?,?);";
    private $update = "UPDATE urgencia SET urg_nome = ?, urg_cor = ? WHERE urg_id = ?";
    private $delete = "DELETE FROM urgencia WHERE urg_id = ?";
    private $select = "SELECT * FROM urgencia";
    private $select_by_id = "SELECT * FROM urgencia WHERE urg_id = ?";
    private $select_by_urgencia = "SELECT * FROM urgencia WHERE urg_nome = ?";
    
    function insert($objUrgencia){
        //conectando ao banco
        $objConn = new Conexao();
        $conn = $objConn->conecta();
        
        //criando o preparedStatment
        
        if ($stm = $conn->prepare($this->insert)) {

            $stm->bind_param("ss", $objUrgencia->getNome(),$objUrgencia->getCor());
            
            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        } else {
            return false;
        }
    }
    
    function update($objUrgencia){
        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->update)) {

            $stm->bind_param("ssi", $objUrgencia->getNome(),$objUrgencia->getCor(), $objUrgencia->getId());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }
    
    function delete($id){
        $objConn = new Conexao();
        $conn = $objConn->conecta();
        
        if ($stm = $conn->prepare($this->delete)) {

            $stm->bind_param("i", $id);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }
    
    function findAllUrgencias(){
        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $nome, $cor, $tempo);

            //para cada resultado encontrado
            $i = 0;
            $lista = array();
            while ($stm->fetch()) {
//                echo "Id: ".$id." Descrição: ".$descricao."<br>";
                $objUrgencia = new UrgenciaModel();
                
                $objUrgencia->setId($id);
                $objUrgencia->setNome($nome);
                $objUrgencia->setCor($cor);
                $objUrgencia->setTempo($tempo);
                    
                $lista[] = $objUrgencia;
            }

            $conn->close();
            return $lista;
        }
    }
    
    function findUrgenciaById($id){
        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $objUrgencia = new UrgenciaModel();
        if ($stm = $conn->prepare($this->select_by_id)) {
            
            $stm->bind_param('i', $id);
            
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $nome, $cor, $tempo);

            //para cada resultado encontrado
            $i = 0;
            $lista = array();
            while ($stm->fetch()) {
//                echo "Id: ".$id." Descrição: ".$descricao."<br>";
                $objUrgencia = new UrgenciaModel();
                
                $objUrgencia->setId($id);
                $objUrgencia->setNome($nome);
                $objUrgencia->setCor($cor);
                $objUrgencia->setTempo($tempo);
                    
                return $objUrgencia;
            }

            $conn->close();
            return $objUrgencia;
        }
    }
    
    function existe($urgencia){
        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select_by_urgencia)) {
            
            $stm->bind_param('s', $urgencia);
            
            $stm->execute();

            while ($stm->fetch()) {
                return true;
            }
            $conn->close();
            return false;
        }
        return false;
    }
}

